# This file creates the variables for analyses
# Set working directory
setwd("/Users/ericguntermann/Documents/Papers/Representation of Party Preferences/Replication")

set.seed(123)

load("cses.Rda")
cses <- cses1_4
cses <- cses[-which(cses$countryyear %in% c("Japan 2013")),] # Remove upper house election
## Only keep parliamentary and semi-presidential regimes
cses <- cses[!is.na(cses$regime), ]
cses <- cses[cses$regime<2,]
cses <- cses[order(cses$countryyear),]


## Create a matrix of like dislike scores with weights
cses.ld <- cses[,c(4,11:19, 62:64)]

# Remove people who expressed no preferences
cses.ld[is.na(cses.ld)] <- 11
rowtot <- rowSums(cses.ld[,2:10], na.rm=T)
cses.ld <- cses.ld[!rowtot==99,]
cses.ld[cses.ld==11] <-NA
cses <- cses[!rowtot==99,]

## Create overall weight (sample, demographic, and political)
cses.ld$weight <- cses.ld$sampleweight*cses.ld$demoweight*cses.ld$polweight

# Criterion 1: What proportion of people in each country's first choice party ended up in government?
cses.ld2 <- data.frame(cses.ld)
cses.ld2[is.na(cses.ld2)] <- 0


# Create a vector of first choice parties for all individuals
# I used which.is.max, which breaks ties at random
library(nnet)
first <- apply(cses.ld2[,2:10],1, which.is.max)

# Cabinet data
incabinet <- cses[,c(4,21:29)]


# Is the most liked party in government (for each respondent)?
firstincabinet <- vector()
for(i in 1:length(first)){
firstincabinet[i] <- incabinet[i,first[i]+1]
}

# Get proportion whose most liked party is in cabinet for each election
propfic <- vector()
for (i in 1:length(unique(cses$countryyear))){
  propfic[i] <- weighted.mean(as.numeric(firstincabinet[cses$countryyear == unique(cses$countryyear)[i]]), cses.ld$weight[cses.ld$countryyear==unique(cses.ld$countryyear)[i]], na.rm=T)
}


# Criterion 2: 
## Calculate mean like-dislike scores by country-year

cses.ld.ag <- matrix(nrow=length(unique(cses.ld$countryyear)), ncol=9)
for(i in 1:length(unique(cses.ld$countryyear))){
  for(j in 1:9){
  cses.ld.ag[i,j] <- weighted.mean(cses.ld[c(cses.ld$countryyear==unique(cses.ld$countryyear)[i]),1+j], cses.ld$weight[cses.ld$countryyear==unique(cses.ld$countryyear)[i]], na.rm=T)
}
}

cses.ld.ag <- as.data.frame(cses.ld.ag)


## Create a vector of most liked parties (aggregate)

cses.ld.ag[,10] <- apply(cses.ld.ag[,1:9], 1, which.max)

mostliked <- cses.ld.ag$V10


## Create aggregate cabinet data matrix

incabinetag <- data.frame(matrix(nrow=length(unique(cses$countryyear)), ncol=9))
for (i in 1:length(unique(incabinet$countryyear))){
  for (j in 1:9){
    incabinetag[i,j] <- incabinet[c(incabinet$countryyear==unique(incabinet$countryyear)[i]),j+1][1]
  }
}
colnames(incabinetag) <- c("incabinet1", "incabinet2", "incabinet3", "incabinet4", "incabinet5", "incabinet6", "incabinet7", "incabinet8", "incabinet9")



## Create logical vector indicating whether the most preferred party is in cabinet
for (i in 1:length(unique(cses$countryyear))){ 
  incabinetag$isin[i] <- incabinetag[i, mostliked[i]]==1
}


# Criterion 3: How liked are governing parties compared to opposition parties?
## Create matrix of governing parties
gov <- cses[, c(4, 21:29)]

## Create matrix of non-governing parties
nongov <- cses[, c(4, 21:29)]
nongov[nongov==1] <- 2
nongov[nongov==0] <- 1
nongov[nongov==2] <- 0

## Create matrix of aggregate weights for governing parties
gweights <- cses[,50:58]*gov[,2:10]
gweightstot <- apply(gweights, 1, sum, na.rm=T)
gweights <- gweights/gweightstot


## Create matrix of weights for non-governing parties
ngweights <- cses[, 50:58]*nongov[, 2:10]
ngweightstot <- apply(ngweights, 1, sum, na.rm=T)
ngweights <- ngweights/ngweightstot


## Overall like dislike score for parties in parliament that are in cabinet
cses.ld2 <- cses.ld[, 2:10]
ldcabinet <- gweights*cses.ld2
ldoverallc <- apply(ldcabinet, 1, sum, na.rm=T)

## Overall like dislike score for parties in parliament that aren't in cabinet

ldnoncabinet <- ngweights*cses.ld2
ldoverallnc <- apply(ldnoncabinet, 1, sum, na.rm=T)

## Create vector of differences between like-dislike scores for parties in cabinet and parties not in cabinet

ldcnc <- data.frame(ldoverallc, ldoverallnc)
lddiff <- vector()
for (i in 1:length(cses$countryyear)){
lddiff[i] <- ldcnc[i,1]-ldcnc[i, 2]
}

lddiffag <- vector()
for(i in 1:length(unique(cses$countryyear))){
  lddiffag[i] <- weighted.mean(lddiff[cses$countryyear==unique(cses$countryyear)[i]], cses.ld$weight[cses.ld$countryyear==unique(cses.ld$countryyear)[i]], na.rm=T) 
}
lddiff <- lddiffag



# Calculate number of parties in government

ningov <- function(x){
  return(sum(x==1,na.rm=T))
}
ngov <- apply(incabinetag[,1:9], 1, ningov)


## Show data
# Create dataset
criteria <- data.frame(countryyear=unique(cses$countryyear), mostliked=incabinetag$isin, lddiff, propfic)
for (i in 1:length(unique(cses$countryyear))){
  criteria$proportional[i] <- cses$proportional[cses$countryyear==unique(cses$countryyear)[i]][1]
}

for (i in 1:length(unique(cses$countryyear))){
  criteria$regime[i] <-  cses$regime[cses$countryyear==unique(cses$countryyear)[i]][1]
}

for (i in 1:length(unique(cses$countryyear))){
  criteria$mdm[i] <-  cses$mdm[cses$countryyear==unique(cses$countryyear)[i]][1]
}
for (i in 1:length(unique(cses$countryyear))){
  criteria$gallagher[i] <- cses$gallagher[cses$countryyear==unique(cses$countryyear)[i]][1]
}

for (i in 1:length(unique(cses$countryyear))){
  criteria$freedomhouse[i] <-  cses$freedomhouse[cses$countryyear==unique(cses$countryyear)[i]][1]
}

for (i in 1:length(unique(cses$countryyear))){
  criteria$gdppercap[i] <-  cses$gdppercap[cses$countryyear==unique(cses$countryyear)[i]][1]
}


criteria$ngov <- ngov
criteria$coalition <- as.factor(ngov>1)
# Adjust number of parties in Australia. The Liberal and National Parties ran together.
criteria$ngov[criteria$countryyear=="Australia 1996"] <- 1
criteria$ngov[criteria$countryyear=="Australia 2004"] <- 1
library(foreign)
write.dta(criteria, "criteria.dta")

# This selection produces numbers mentioned in Discussion section

# Select only PR vs. non-PR elections
cses.ld.ag <- cses.ld.ag[-which(criteria$countryyear %in% c("Thailand 2001", "Thailand 2011","Japan 1996", "Japan 2013")),]
criteria <- criteria[-which(criteria$countryyear %in% c("Thailand 2001", "Thailand 2011","Japan 1996", "Japan 2013")),]
cses <- cses[-which(cses$countryyear %in% c("Thailand 2001", "Thailand 2011","Japan 1996", "Japan 2013")),]

# Proportion of PR/non-PR systems with coalition governments
mean(criteria$ngov[criteria$proportional==1]>1)

mean(criteria$ngov[criteria$proportional==0]>1)

# Calculate average rating of different types of parties
seatsag <- matrix(ncol=9, nrow=0)
# Get propportion of seats in cabinet
for(i in 1:length(unique(cses$countryyear))){
  seatsag <- rbind(seatsag, cses[cses$countryyear==unique(cses$countryyear)[i],30:38][1,])
}
#Determine which party has most seats
firstparty <- apply(seatsag,1,which.max)
fpmat <- matrix(NA,ncol=9, nrow=84)
for(i in 1:nrow(fpmat)){
fpmat[i,firstparty[i]] <- 1
}

# Average rating of governing party (non-coalition)
mean(as.vector(as.matrix(cses.ld.ag[criteria$ngov==1,1:9]*fpmat[criteria$ngov==1])), na.rm=T)

# Average rating of governing party with most seats (coalition)
mean(as.vector(as.matrix(cses.ld.ag[criteria$ngov>1,1:9]*fpmat[criteria$ngov>1])), na.rm=T)

# Average rating of opposition parties
opmat <- matrix(NA, ncol=9, nrow=84)
  opmat[seatsag==0] <- 1

mean(as.vector(as.matrix(cses.ld.ag[,1:9]*opmat)), na.rm=T)

# Average ratings of coalition parties with less than 20% of seats
nfmat <- matrix(NA,ncol=9, nrow=84)
is <- matrix(0,84,9)
is[seatsag>0 & seatsag<0.2] <- 1
nfmat[is==1] <- 1


mean(as.vector(as.matrix(cses.ld.ag[criteria$ngov>1,1:9]*nfmat[criteria$ngov>1,])), na.rm=T)


